/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.kelas;

import com.ivanbiz.account.Account;
import com.ivanbiz.account.AccountService;
import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.matapelajaran.MataPelajaran;
import com.ivanbiz.matapelajaran.MataPelajaranService;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.ruang.Ruang;
import com.ivanbiz.ruang.RuangService;
import com.ivanbiz.siswa.Siswa;
import com.ivanbiz.siswa.SiswaService;
import com.ivanbiz.tools.IvanBiz;
import com.ivanbiz.user.User;
import com.ivanbiz.workflow.Workflow;
import com.ivanbiz.workflow.WorkflowService;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class KelasAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward kelasList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        try {
            List listKelas = kelasService.loadAll();
            request.setAttribute(SISConstants.LIST_KELAS, listKelas);
            List listAccount = accountService.loadAll();
            request.setAttribute(SISConstants.LIST_ACCOUNT, listAccount);
            List listRuang = ruangService.loadAll();
            request.setAttribute(SISConstants.LIST_RUANG, listRuang);
            List listWorkflow = workflowService.loadAll();
            request.setAttribute(SISConstants.LIST_WORKFLOW, listWorkflow);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.KELAS_LIST);
    }

    public ActionForward kelasAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        KelasForm kelasForm = (KelasForm) form;
        Kelas kelas = new Kelas();
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER);
        try {
            Account account = accountService.loadById(kelasForm.getAccount());
            kelas.setAccount(account);
            Ruang ruang = ruangService.loadById(kelasForm.getRuang());
            kelas.setRuang(ruang);
            Workflow workflow = workflowService.loadById(kelasForm.getWorkflow());
            kelas.setWorkflow(workflow);
            kelas.setCreatedBy(user.getNama());
            kelas.setCreatedDt(new Date());
            kelasService.saveKelas(kelas);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.KELAS);
    }

    public ActionForward kelasEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        KelasForm kelasForm = (KelasForm) form;
        try {
            Kelas kelas = kelasService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            kelasForm.setAccount(kelas.getAccount().getId());
            kelasForm.setRuang(kelas.getRuang().getId());
            kelasForm.setWorkflow(kelas.getWorkflow().getId());
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.KELAS_EDIT);
    }

    public ActionForward kelasUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        KelasForm kelasForm = (KelasForm) form;
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER);
        try {
            Kelas kelas = kelasService.loadById(kelasForm.getId());
            Account account = accountService.loadById(kelasForm.getAccount());
            kelas.setAccount(account);
            Ruang ruang = ruangService.loadById(kelasForm.getRuang());
            kelas.setRuang(ruang);
            Workflow workflow = workflowService.loadById(kelasForm.getWorkflow());
            kelas.setWorkflow(workflow);
            kelas.setUpdateBy(user.getNama());
            kelas.setUpdateDt(new Date());
            kelasService.updateKelas(kelas);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.KELAS);
    }

    public ActionForward kelasDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        try {
            Kelas kelas = kelasService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            kelasService.deleteKelas(kelas);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.KELAS);
    }

    public ActionForward accountKelasList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountKelasService accountKelasService = (AccountKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_KELAS_SERVICE);
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
        try {
            List listAccountKelas = accountKelasService.loadAll();
            request.setAttribute(SISConstants.LIST_ACCOUNT_KELAS, listAccountKelas);
            List listKelas = kelasService.loadAll();
            request.setAttribute(SISConstants.LIST_KELAS, listKelas);
            List listSiswa = siswaService.loadAll();
            request.setAttribute(SISConstants.LIST_SISWA, listSiswa);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_KELAS_LIST);
    }

    public ActionForward accountKelasAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountKelasService accountKelasService = (AccountKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_KELAS_SERVICE);
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
        KelasForm kelasForm = (KelasForm) form;
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER);
        try {
            Kelas kelas = kelasService.loadById(kelasForm.getKelas());
            Siswa siswa = siswaService.loadById(kelasForm.getSiswa());
            AccountKelas accountKelas = new AccountKelas();
            accountKelas.setKelas(kelas);
            accountKelas.setSiswa(siswa);
            accountKelas.setCreatedBy(user.getNama());
            accountKelas.setCreatedDt(new Date());
            accountKelasService.saveAccountKelas(accountKelas);
            kelasService.saveKelas(kelas);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_KELAS);
    }

    public ActionForward accountKelasDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountKelasService accountKelasService = (AccountKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_KELAS_SERVICE);
        try {
            AccountKelas accountKelas = accountKelasService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            accountKelasService.deleteAccountKelas(accountKelas);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_KELAS);
    }

    public ActionForward mataPelajaranKelasList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranKelasService mataPelajaranKelasService = (MataPelajaranKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_KELAS_SERVICE);
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        try {
            List listMataPelajaranKelas = mataPelajaranKelasService.loadAll();
            request.setAttribute(SISConstants.LIST_MATA_PELAJARAN_KELAS, listMataPelajaranKelas);
            List listKelas = kelasService.loadAll();
            request.setAttribute(SISConstants.LIST_KELAS, listKelas);
            List listMataPelajaran = mataPelajaranService.loadAll();
            request.setAttribute(SISConstants.LIST_MATA_PELAJARAN, listMataPelajaran);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN_KELAS_LIST);
    }

    public ActionForward mataPelajaranKelasAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranKelasService mataPelajaranKelasService = (MataPelajaranKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_KELAS_SERVICE);
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        KelasForm kelasForm = (KelasForm) form;
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER);
        try {
            Kelas kelas = kelasService.loadById(kelasForm.getKelas());
            MataPelajaran mataPelajaran = mataPelajaranService.loadById(kelasForm.getMataPelajaran());
            MataPelajaranKelas mataPelajaranKelas = new MataPelajaranKelas();
            mataPelajaranKelas.setKelas(kelas);
            mataPelajaranKelas.setMatapelajaran(mataPelajaran);
            mataPelajaranKelas.setCreatedBy(user.getNama());
            mataPelajaranKelas.setCreatedDt(new Date());
            mataPelajaranKelasService.saveMataPelajaranKelas(mataPelajaranKelas);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN_KELAS);
    }

    public ActionForward mataPelajaranKelasDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranKelasService mataPelajaranKelasService = (MataPelajaranKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_KELAS_SERVICE);
        try {
            MataPelajaranKelas mataPelajaranKelas = mataPelajaranKelasService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            mataPelajaranKelasService.deleteMataPelajaranKelas(mataPelajaranKelas);
        } catch (Exception ex) {
            Logger.getLogger(KelasAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN_KELAS);
    }
}